package com.hz.controller;

import com.hz.common.CommonPage;
import com.hz.common.CommonResult;

import com.hz.mbg.entity.TbBanner;
import com.hz.service.BannerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// Banner相关操作Controller
@Api(tags = "BannerController", description = "Banner相关操作")
@Controller
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    private static final Logger LOGGER = LoggerFactory.getLogger(BannerController.class);

    @ApiOperation("获取Banner列表按照bannerposition升序")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<TbBanner>> getAllList() {
        return CommonResult.success(bannerService.listBybannerposition());
    }

    @ApiOperation("添加Banner")
    @RequestMapping(value = "/admin/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody TbBanner banner) {
        CommonResult commonResult;
        int count = bannerService.add(banner);
        if (count == 1) {
            commonResult = CommonResult.success(banner);
            LOGGER.debug("成功添加Bannner:{}", banner);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("添加Bannner失败原因:{}", banner);
        }
        return commonResult;
    }

    @ApiOperation("更新指定Banner信息")
    @RequestMapping(value = "/admin/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    private CommonResult update(@PathVariable("bannerid") Integer bannerid, @RequestBody TbBanner banner) {
        CommonResult commonResult;
        int count = bannerService.update(bannerid, banner);
        if (count == 1) {
            commonResult = CommonResult.success(banner);
            LOGGER.debug("成功更新Bannner:{}", banner);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("更新Bannner失败原因:{}", banner);
        }
        return commonResult;
    }

    @ApiOperation("删除指定Banner")
    @RequestMapping(value = "/admin/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable("bannerid") Integer bannerid) {
        int count = bannerService.delete(bannerid);
        if (count == 1) {
            LOGGER.debug("成功删除Bannner:bannerid={}", bannerid);
            return CommonResult.success(null);
        } else {
            LOGGER.debug("删除Bannner:bannerid={}失败", bannerid);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("获取指定Bannner详情")
    @RequestMapping(value = "/admin/{bannerid}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<TbBanner> get(@PathVariable("bannerid") Integer bannerid) {
        return CommonResult.success(bannerService.get(bannerid));
    }

    @ApiOperation("分页查询Bannner列表")
    @RequestMapping(value = "/admin/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<TbBanner>> getList(@RequestParam(value = "pageNum", defaultValue = "1")
                                                      @ApiParam("页码") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "2")
                                                      @ApiParam("每页数量") Integer pageSize) {
        List<TbBanner> banners = bannerService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(banners));
    }
}
